package safeCommitter.test.finder.core;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;

import safeCommitter.utils.SCUtils;

public abstract class ASCJavaFinder {

	protected ISCMethodRepository repository;
	private IJavaSearchScope scope;
	private SearchRequestor requestor;
	private Boolean userCancelled = Boolean.FALSE;

	public ASCJavaFinder(final IJavaProject javaProject, final ISCMethodRepository methodRepository) {
		IPackageFragmentRoot[] packageRoots;
		try {
			packageRoots = javaProject.getPackageFragmentRoots();
			scope = SCUtils.buildScope(packageRoots);
			repository = methodRepository;
		} catch (JavaModelException e) {
			e.printStackTrace();
		}

	}

	protected void performJavaSearch(final IMethod method, final boolean recursive) {
		SearchPattern pattern = SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES);
		SearchEngine searchEngine = new SearchEngine();
		try {
			searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
					requestor, null);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	public void setRequestor(final SearchRequestor requestor) {
		this.requestor = requestor;
	}

	public Boolean getUserCancelled() {
		return userCancelled;
	}

	public void setUserCancelled(final Boolean userCancelled) {
		this.userCancelled = userCancelled;
	}

}
